#!/usr/bin/env node
require('dotenv').config();
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

function runCommand(command) {
  try {
    console.log(`执行命令: ${command}`);
    // 在Windows下，使用cmd.exe执行命令
    execSync(command, { stdio: 'inherit', cwd: __dirname, shell: 'cmd.exe' });
    return true;
  } catch (error) {
    console.error(`命令执行失败: ${command}`);
    console.error(error.message);
    return false;
  }
}

function showHelp() {
  console.log('使用方法: node test-cli.js [command]');
  console.log('命令列表:');
  console.log('  install    - 安装依赖');
  console.log('  test       - 运行所有测试');
  console.log('  unit       - 运行单元测试');
  console.log('  integration - 运行集成测试');
  console.log('  coverage   - 生成测试覆盖率报告');
  console.log('  help       - 显示帮助信息');
}

const args = process.argv.slice(2);

if (args.length === 0 || args[0] === 'help') {
  showHelp();
  process.exit(0);
}

const command = args[0];

switch (command) {
  case 'install':
    runCommand('pnpm install');
    break;
  case 'test':
    runCommand('pnpm test');
    break;
  case 'unit':
    runCommand('pnpm test __tests__/apiClient.test.js');
    break;
  case 'integration':
    runCommand('pnpm test __tests__/integration.test.js');
    break;
  case 'coverage':
    runCommand('pnpm test:coverage');
    break;
  default:
    console.error(`未知命令: ${command}`);
    showHelp();
    process.exit(1);
}